package com.ewayde.base.statemachine.builder;

import com.ewayde.base.statemachine.Action;
import com.ewayde.base.statemachine.Condition;
import com.ewayde.base.statemachine.State;
import com.ewayde.base.statemachine.Transition;
import com.ewayde.base.statemachine.impl.StateHelper;

import java.util.Map;

/**
 * @author yanwei li create  2024-05-28 22:39
 */
public class TransitionBuilderImpl<S,E,C> implements TransitionBuilder<S,E,C>,From<S,E,C>,To<S,E,C>,On<S,E,C>{

    private Map<S, State<S,E,C>> stateMap;

    private State<S,E,C> sourceState;

    private State<S,E,C> targetState;

    private Transition<S,E,C> transition;

    public TransitionBuilderImpl(Map<S,State<S,E,C>> stateMap){
        this.stateMap = stateMap;
    }

    @Override
    public From<S, E, C> from(S stateId) {
        sourceState = StateHelper.getState(stateMap,stateId);
        return this;
    }

    @Override
    public To<S, E, C> to(S stateId) {
        targetState = StateHelper.getState(stateMap,stateId);
        return this;
    }


    @Override
    public void perform(Action<S, E, C> action) {
        transition.setAction(action);
    }


    @Override
    public On<S, E, C> on(E event) {
        transition = sourceState.addTransition(event, targetState);
        return this;
    }

    @Override
    public When<S,E,C> when(Condition<C> condition) {
        transition.setCondition(condition);
        return this;
    }
}
